package com.example.NIO;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NIOFileChannel01 {
    public static void main(String[] args) throws IOException {
        String str = "hello word";
//        File file = new File("C:\\Users\\Administrator\\Desktop");
        // 创建一个 输出流 ->channel
        FileOutputStream outputStream =
                new FileOutputStream("C:\\Users\\Administrator\\Desktop\\1.txt");

        // 通过outputStream 获取对应的 FileChannel
        // 这个 fileChannel 真实类型是FileChannelImpl
        FileChannel fileChannel = outputStream.getChannel();

        // 创建一个缓冲区 ByteBuffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

//        将 Str 放入 byteBuffer
        byteBuffer.put(str.getBytes());

        // 对 byteBuffer 进行 flip
        byteBuffer.flip();

        // 将 byteBuffer 数据写入 fileChannel
        fileChannel.write(byteBuffer);
        outputStream.close();
    }
}
